package net.trustie.model;

import java.text.SimpleDateFormat;
import java.util.List;

import net.trustie.utils.DateHandler;
import net.trustie.utils.StringHandler;

import org.jsoup.Jsoup;

import us.codecraft.webmagic.model.annotation.ExtractBy;
import core.AfterExtractor;
import core.Page;
import core.ValidateExtractor;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;

public class softpedia_Model implements AfterExtractor, ValidateExtractor {

	private String project_url;

	private String extractTime;

	private int history;

	private String tag;
	

	@ExtractBy("//*div[@class='wrapper posrel']/div[@role='main']/div[@class='container_48']/div[@class='pagetop grid_48 pagetop2 srcnobg minhigh80']/h1/span[@itemprop='name']/text()")
	private String project_name;

	@ExtractBy("//*div[@class='wrapper posrel']/div[@role='main']/div[@class='container_48']/div[@class='pagetop grid_48 pagetop2 srcnobg minhigh80']/div[@class='nv_nutshell col-blue mgbot_20']/text()")
	private String project_context;

	private String desc_path = "div>div";
	@ExtractBy("//*div[@class='wrapper posrel']/div[@role='main']/div[@class='container_48']/div[@class='grid_31 prefix_2 sepbrd4 fr']/div[@class='storybox1 legible']/html()")
	private String description_html;
	private String project_description;
	private String tags;

	@ExtractBy("//*div[@class='wrapper posrel']/div[@role='main']/div[@class='container_48']/div[@class='grid_15 fl sepbrd4']/div[@class='progcolbx2']/div[@class='dlbox1 nosel']/a[@id='dlbtn1']/p[@class='col-blue']/span[@id='_dlspan']/text()")
	private String download_num = "";

	@ExtractBy("//*div[@class='wrapper posrel']/div[@role='main']/div[@class='container_48']/div[@class='grid_31 prefix_2 sepbrd4 fr']/div[@class='storybox1 legible']/div[@itemprop='review']/p[@class='reviewedby1 mgtop_20']/allText()")
	private String tmp_time;
	private String post_time;

	@ExtractBy("//*div[@class='wrapper posrel']/div[@role='main']/div[@class='container_48']/div[@class='grid_31 prefix_2 sepbrd4 fr']/div[@class='storybox1 legible']/div[@itemprop='review']/p[@class='runson mgtop_10 ellip']/text()")
	private String system;

	@ExtractBy("//*div[@class='wrapper posrel']/div[@role='main']/div[@class='container_48']/div[@class='grid_31 prefix_2 sepbrd4 fr']/dl[@class='pspec2015']/dd/a[@rel='nofollow']/strong[@itemprop='publisher']/text()")
	private String author;

	@ExtractBy("//*div[@class='wrapper posrel']/div[@role='main']/div[@class='container_48']/div[@class='grid_31 prefix_2 sepbrd4 fr']/dl[@class='pspec2015']/dd/a[@rel='nofollow']/@href")
	private String author_url;

	private String category_path = "dd.ellip";
	private String user_rating_path = "div.hdrating1>span.usr>span";
	private String licence_type_path = "dd>span.licspan";
	@ExtractBy("//*div[@class='wrapper posrel']/div[@role='main']/div[@class='container_48']/div[@class='grid_31 prefix_2 sepbrd4 fr']/dl[@class='pspec2015']/html()")
	private String tables="";
	private String category="";
	private String user_rate[];
	private String user_rate_num;
	private String licence_type;

	@Override
	public void afterProcess(Page page) {
		// TODO Auto-generated method stub
		this.project_url = page.getPageUrl();
		this.extractTime = DateHandler.getExtractTime();

		// post_time的处理
		if (tmp_time != null) {
			String time = StringHandler.matchRightString(tmp_time,
					"[a-zA-Z]+\\s(\\d{1}|\\d{2})[a-zA-Z]{2},\\s\\d{4}");
			String[] ful_time = time.split(",");
			String[] times = ful_time[0].split(" ");
			String month = DateHandler.changeFullMonthToNum(times[0]);
			String date = StringHandler.matchRightString(times[1],
					"\\d{1}|\\d{2}");
			String year = ful_time[1];
			this.post_time = year + "-" + month + "-" + date;
			this.post_time = DateHandler.standardForDate(this.post_time);
		}

		// project_description的处理
		if (this.description_html != null) {
			project_description = jsoupHandler(description_html, desc_path);
		}

		// 抽取table的信息
		if (!tables.isEmpty()) {
			category = jsoupHandler(tables, category_path);
			String tmp_user_rate = jsoupHandler(tables, user_rating_path);
			if (tmp_user_rate != null) {
				user_rate = tmp_user_rate.split(" ");
				if (this.user_rate.length == 0) {
					this.user_rate_num = user_rate[0] + "/" + user_rate[1];
				}
			}
			licence_type = jsoupHandler(tables, licence_type_path);
		}
		// 处理download_num;
		if(!download_num.isEmpty()){
		String[] tmp = this.download_num.split(",");
		String tmp_num = "";
		for (String str : tmp) {
			tmp_num += str;
		}
		this.download_num = tmp_num;
		}
		// 处理tags
		this.tags = jsoupHandler(description_html,
				"div>p.tagsnew.mgtop_10>span");
		this.tags = this.tags.substring(1, this.tags.length() - 1);
		String[] tmp_tags = tags.split("#");
		this.tag = StringHandler.combineTags(tmp_tags);
	}

	@Override
	public void validate(Page page) {
		if (!page.getResultItems().isSkip()) {
			if (!StringHandler.isAllNotBlank(this.project_name,
					this.project_description, this.author)) {
				page.setSkip(true);
				return;
			}
		}
	}

	private String jsoupHandler(String html, String select_path) {
		Document doc = Jsoup.parse(html);
		Elements ele = doc.select(select_path);
		return ele.text();
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String getProject_url() {
		return project_url;
	}

	public void setProject_url(String project_url) {
		this.project_url = project_url;
	}

	public String getExtractTime() {
		return extractTime;
	}

	public void setExtractTime(String extractTime) {
		this.extractTime = extractTime;
	}

	public String getProject_name() {
		return project_name;
	}

	public void setProject_name(String project_name) {
		this.project_name = project_name;
	}

	public String getProject_context() {
		return project_context;
	}

	public void setProject_context(String project_context) {
		this.project_context = project_context;
	}

	public String getProject_description() {
		return project_description;
	}

	public void setProject_description(String project_description) {
		this.project_description = project_description;
	}

	public String getDownload_num() {
		return download_num;
	}

	public void setDownload_num(String download_num) {
		this.download_num = download_num;
	}

	public String getPost_time() {
		return post_time;
	}

	public void setPost_time(String post_time) {
		this.post_time = post_time;
	}

	public String getSystem() {
		return system;
	}

	public void setSystem(String system) {
		this.system = system;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getAuthor_url() {
		return author_url;
	}

	public void setAuthor_url(String author_url) {
		this.author_url = author_url;
	}

	public int getHistory() {
		return history;
	}

	public void setHistory(int history) {
		this.history = history;
	}

	public String getDesc_path() {
		return desc_path;
	}

	public void setDesc_path(String desc_path) {
		this.desc_path = desc_path;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String[] getUser_rate() {
		return user_rate;
	}

	public void setUser_rate(String[] user_rate) {
		this.user_rate = user_rate;
	}

	public String getLicence_type() {
		return licence_type;
	}

	public void setLicence_type(String licence_type) {
		this.licence_type = licence_type;
	}

	public String getTmp_time() {
		return tmp_time;
	}

	public void setTmp_time(String tmp_time) {
		this.tmp_time = tmp_time;
	}

	public String getUser_rate_num() {
		return user_rate_num;
	}

	public void setUser_rate_num(String user_rate_num) {
		this.user_rate_num = user_rate_num;
	}

}
